package com.bestcem.xm.common.core.domain.web;

import com.bestcem.xm.common.core.enums.CommonErrorEnum;
import com.zy.dp.proto.base.v1.Code;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * service 层返回结果封装类
 * 最后更新时间: 2021年6月4日15:05:55
 *
 * @author gy.liu
 * @version v3.0
 * @date 2021-05-08
 */
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Data
public class XmResultJson<T> {

    @ApiModelProperty(value = "具体数据")
    private T data;

    @ApiModelProperty(value = "提示信息")
    private String msg;

    @ApiModelProperty(value = "错误码")
    private Integer code;

    public static <T> XmResultJson<T> success() {
        return success(null, "");
    }

    public static <T> XmResultJson<T> success(T data) {
        return success(data, "");
    }

    public static <T> XmResultJson<T> success(T data, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setCode(0);
        return serviceResult;
    }

    public static <T> XmResultJson<T> success(Integer code, T data, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setCode(code);
        return serviceResult;
    }

    public static <T> XmResultJson<T> fail(Integer code, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setCode(code);
        serviceResult.setMsg(msg);
        return serviceResult;
    }

    public static <T> XmResultJson<T> fail(T data, Integer code, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setData(data);
        serviceResult.setCode(code);
        serviceResult.setMsg(msg);
        return serviceResult;
    }

    /**
     * 构造失败返回参数
     *
     * @param errorEnum 非空
     * @param <T>       泛型
     * @return xmResultJson
     */
    public static <T> XmResultJson<T> fail(CommonErrorEnum errorEnum) {
        if (errorEnum == null) {
            throw new RuntimeException("错误码信息为空");
        }
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setCode(errorEnum.getCode());
        serviceResult.setMsg(errorEnum.getMsg());
        return serviceResult;
    }

    /**
     * 构造失败返回参数
     *
     * @param errorEnum 错误码信息
     * @param msg       错误描述信息
     * @param <T>
     * @return
     */
    public static <T> XmResultJson<T> fail(CommonErrorEnum errorEnum, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        if (errorEnum == null) {
            serviceResult.setCode(Code.ERROR_VALUE);
        } else {
            serviceResult.setCode(errorEnum.getCode());
            serviceResult.setMsg(errorEnum.getMsg());
        }
        serviceResult.setMsg(msg);
        return serviceResult;
    }

    /**
     * 构造失败返回参数
     *
     * @param data      数据
     * @param errorEnum 错误码信息
     * @param msg       错误描述信息
     * @param <T>
     * @return
     */
    public static <T> XmResultJson<T> fail(T data, CommonErrorEnum errorEnum, String msg) {
        XmResultJson<T> serviceResult = new XmResultJson<>();
        serviceResult.setData(data);
        if (errorEnum == null) {
            serviceResult.setCode(Code.ERROR_VALUE);
        } else {
            serviceResult.setCode(errorEnum.getCode());
        }
        serviceResult.setMsg(msg);
        return serviceResult;
    }

    public static <T> XmResultJson<T> handleServiceResult(ServiceResult<T> serviceResult) {
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        } else {
            return XmResultJson.fail(-1, serviceResult.getMsg());
        }
    }

    public XmResultJson<T> setErrorCode(Integer code) {
        this.code = code;
        return this;
    }
}

